package com.ymatou.iapi.payment.testcase.old;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.*;
import com.ymatou.iapi.payment.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.model.PPBussinessOrder;
import com.ymttest.database.model.PPPayment;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.PaymentDefaultWapper;
import com.ymttest.utils.TestDataManager;
/**
*支付执行通知
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ExecutePayNotify{
	private static ExecutePayNotifyBean executepaynotifyBean;
	private static ExecutePayNotifyCall executepaynotifyCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("支付执行通知");
	}

	@Before
	public void caseUp() {
		executepaynotifyBean=new ExecutePayNotifyBean();
		executepaynotifyCall=new ExecutePayNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(executepaynotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_001() {
		Logger.start(true,"调用执行支付通知paytype10,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	//这个case返回ok，不会实际执行
	public void Tc_ExecutePayNotify_002() {
		Logger.start(true,"不存在支付单,失败");
		try {
			executepaynotifyBean.setPaymentId("aaa");
			executepaynotifyCall.setData(executepaynotifyBean);
//			executepaynotifyCall.addHeader("Mock", "1");
//			Logger.comment("设置交易返回MOCK");
//			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
//			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
//			String mockid = TestDataManager.getRandomUUID().toString();
//			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ExecutePayNotify_003() {
		Logger.start(true,"已支付支付单,幂等");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
//			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ExecutePayNotify_004() {
		Logger.start(true,"回调状态1无回调地址,更新回调状态2");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			aqb.setNotifyUrl("");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			new PaymentDefaultWapper().updateNotifyStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			PPPayment payment = new PaymentDefaultWapper().selectPPPaymentByPaymentId(pid).get(0);
			Logger.verifyEquals("2", payment.getNotifyStatus(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ExecutePayNotify_005() {
		Logger.start(true,"回调状态1有回调地址,通知支付调用方,更新回调状态2");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			new PaymentDefaultWapper().updateNotifyStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			PPPayment payment = new PaymentDefaultWapper().selectPPPaymentByPaymentId(pid).get(0);
			Logger.verifyEquals("2", payment.getNotifyStatus(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ExecutePayNotify_006() {
		Logger.start(true,"回调状态2,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			new PaymentDefaultWapper().updateNotifyStatus(pid, "2");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_007() {
		Logger.start(true,"调用执行支付通知paytype13,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("13");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_008() {
		Logger.start(true,"调用执行支付通知paytype14,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("14");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_009() {
		Logger.start(true,"调用执行支付通知paytype15,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("15");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_010() {
		Logger.start(true,"调用执行支付通知paytype50,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("50");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_011() {
		Logger.start(true,"调用执行支付通知paytype60,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("60");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_012() {
		Logger.start(true,"调用执行支付通知paytype70,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("70");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_013() {
		Logger.start(true,"调用执行支付通知paytype9,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("9");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ExecutePayNotify_014() {
		Logger.start(true,"调用执行支付通知paytype17,执行支付成功回调通知成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("17");
			aqb.setNotifyUrl("http://mockforpay.iapi.ymatou.com/api/Trading/NotifyTradingEvent");
			PaymentProxyCallService.acquireOrder(aqb);
			String pid = PaymentProxyCallService.getPaymentId(aqb);
			new PaymentDefaultWapper().updatePayStatus(pid, "1");
			executepaynotifyBean.setPaymentId(pid);
			executepaynotifyCall.setData(executepaynotifyBean);
			executepaynotifyCall.addHeader("Mock", "1");
			Logger.comment("设置交易返回MOCK");
			executepaynotifyCall.addHeader("MockResult-Trading-Status","OK");
			executepaynotifyCall.addHeader("MockResult-Trading-Body	",FormatUtil.urlEncode("{\"code\":200}"));
			String mockid = TestDataManager.getRandomUUID().toString();
			executepaynotifyCall.addHeader("MockId", mockid);
			executepaynotifyCall.callService();			
			Logger.verifyEquals("ok", executepaynotifyCall.getReturnData(), "验证返回");
			verifyDB(aqb,pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(AcquireOrderBean aqb,String pid) throws Exception {
		Logger.comment("验证pp_payment");
		PPBussinessOrder ppb = new PaymentDefaultWapper().selectPPBussinessorderByTraceId(aqb.getTraceId()).get(0);
		
		String businessoid = ppb.getBussinessOrderId();
		PPPayment ppm = new PaymentDefaultWapper().selectPPPaymentByBussinessOrderId(businessoid).get(0);
		if(ppm.getInstitutionPaymentId()==null){
			Thread.sleep(5000);
			ppm = new PaymentDefaultWapper().selectPPPaymentByBussinessOrderId(businessoid).get(0);
		}
		Logger.verifyEquals(PayOrderEnum.Success.getValue(), ppm.getPayStatus(), "验证PayStatus");
		Logger.comment("验证YmtAccountEntry");
        Map m = new HashMap();
        m.put("bizno", pid);
		List<YmtAccountEntry> actinfos = new AccountDefaultWapper().selectAccountEntryByBizNo(m);
		Logger.verifyEquals(1, actinfos.size(), "验证YmtAccountEntry记录条数");
		YmtAccountEntry actinfo = actinfos.get(0);
		Logger.debug(FormatUtil.beanToGSONString(actinfo));
		Logger.verifyEquals(aqb.getUserId(), actinfo.getUserid().toString(), "验证Userid");
		Logger.verifyEquals(0, actinfo.getAmount().compareTo(new BigDecimal(aqb.getPayPrice())),
				"验证PayPrice:" + aqb.getPayPrice());
		// 入账 type 2
		Logger.verifyEquals("2", actinfo.getEntrytype().toString(), "验证Entrytype");
		Logger.verifyEquals(pid, actinfo.getBizno(), "验证Bizno为PaymentId");
		if (aqb.getBizCode().equals("1")) {
			Logger.verifyEquals("100002", actinfo.getBizcode().toString(), "验证Bizcode");
			Logger.verifyEquals("补款充值", actinfo.getMemo(), "验证Memo");
		} else if (aqb.getBizCode().equals("2")) {
			Logger.verifyEquals("100003", actinfo.getBizcode().toString(), "验证Bizcode");
			Logger.verifyEquals("现金充值", actinfo.getMemo(), "验证Memo");
		} else if (aqb.getBizCode().equals("3")) {
			Logger.verifyEquals("100001", actinfo.getBizcode().toString(), "验证Bizcode");
			Logger.verifyEquals("定金充值", actinfo.getMemo(), "验证Memo");
		} else if (aqb.getBizCode().equals("4")) {
			Logger.verifyEquals("100003", actinfo.getBizcode().toString(), "验证Bizcode");
			Logger.verifyEquals("卖家保证金充值", actinfo.getMemo(), "验证Memo");
		}
	}
}